﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using CMP.ERP.Model;
using CMP.ERP.BLL;

namespace CMP.ERP.Admin.Controllers
{
    public class StockController : Controller
    {
        //
        // GET: /Stock/
        /// <summary>
        /// 实例化StockBLL对象
        /// </summary>
        StockBLL bll = new StockBLL();
        ///// <summary>
        ///// 显示库存
        ///// </summary>
        ///// <returns></returns>
        //public ActionResult Stock_Show(Stock s)
        //{
        //    ViewBag.Commodity_Id = new SelectList(bll.GetCnameAll(), "Commodity_Id", "Commodity_Name");
        //    ViewBag.WareHose_Id = new SelectList(bll.GetWnameAll(), "WareHose_Id", "WareHose_Name");
        //    return View(bll.GetStockAll());
        //}
        /// <summary>
        /// 显示库存
        /// </summar;y>
        int pageSize = 8;
        /// <returns></returns>
        public ActionResult Stock_Show()
        {
            int PageIndex = 1;
            ViewBag.PageIndex = PageIndex;
            int count = bll.GetStockCount();
            if (count % pageSize > 0)
            {
                ViewBag.count = count % pageSize + 1;
            }
            else
            {
                ViewBag.count = count % pageSize;
            }

            ViewBag.Commodity_Id = new SelectList(bll.GetCnameAll(), "Commodity_Id", "Commodity_Name");
            ViewBag.WareHose_Id = new SelectList(bll.GetWnameAll(), "WareHose_Id", "WareHose_Name");

            return View(bll.GetStockAll(pageSize, PageIndex).ToList());
        }
        public ActionResult Stock_ShowPage(int id)
        {

            int PageIndex = id;
            ViewBag.PageIndex = PageIndex;
            int count = bll.GetStockCount();
            if (count % pageSize > 0)
            {
                ViewBag.count = count % pageSize + 1;
            }
            else
            {
                ViewBag.count = count % pageSize;
            }

            ViewBag.Commodity_Id = new SelectList(bll.GetCnameAll(), "Commodity_Id", "Commodity_Name");
            ViewBag.WareHose_Id = new SelectList(bll.GetWnameAll(), "WareHose_Id", "WareHose_Name");

            return View("Stock_Show", bll.GetStockAll(pageSize, PageIndex));
        }
        /// <summary>
        /// 弹出修改库存视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult UptStock(int id)
        {
            Stock s = bll.GetStockById(id);
            ViewBag.Commodity_Id = new SelectList(bll.GetCnameAll(), "Commodity_Id", "Commodity_Name", s.Commodity_Id);
            ViewBag.WareHose_Id = new SelectList(bll.GetWnameAll(), "WareHose_Id", "WareHose_Name", s.WareHose_Id);
            return View(s);
        }
        /// <summary>
        /// 实现修改库存功能
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public ActionResult UptStockDo(Stock s)
        {
            if (bll.UpdStock(s) > 0)
            {
                return Content("<script>alert('更新成功！'); location.href='/Stock/Stock_Show'</script>");
            }
            else
            {
                return Content("<script>alert('更新失败！'); location.href='/Stock/Stock_Show'</script>");
            }
        }
        /// <summary>
        /// 添加功能
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public ActionResult AddStockDo(Stock s)
        {
            if (bll.AddStock(s) > 0)
            {
                return Content("<script>alert('添加成功！'); location.href='/Stock/Stock_Show'</script>");
            }
            else
            {
                return Content("<script>alert('添加失败！'); location.href='/Stock/Stock_Show'</script>");
            }
        }
        //按名称查找商品
        public ActionResult SearchByName(string Commodity_Name, int? WareHose_Id)
        {
            int PageIndex = 1;
            ViewBag.PageIndex = PageIndex;
            int count = bll.GetStockCount();
            if (count % pageSize > 0)
            {
                ViewBag.count = count % pageSize + 1;
            }
            else
            {
                ViewBag.count = count % pageSize;
            }

            ViewBag.Commodity_Id = new SelectList(bll.GetCnameAll(), "Commodity_Id", "Commodity_Name");
            ViewBag.WareHose_Id = new SelectList(bll.GetWnameAll(), "WareHose_Id", "WareHose_Name");

            return View("Stock_Show", bll.sousuo(Commodity_Name, WareHose_Id, pageSize, PageIndex));
        }
    }
}
